/**
 * Definition for a binary tree node.
 * function TreeNode(val) {
 *     this.val = val;
 *     this.left = this.right = null;
 * }
 */
/**
 * @param {TreeNode} root
 * @return {boolean}
 */
var isSymmetric = function(root) {
    if(!root)return true;

    let node1 = root.left,
        node2 = root.right,
        stack1 = [node1],
        stack2 = [node2];
    if((!node1&&node2)||(node1&&!node2))return false;

    //使用队列保存所有节点，确定好 push、shift的时机
    while(stack1.length>0&&stack2.length>0){
        node1 = stack1.shift();
        node2 = stack2.shift();

        if((!node1&&node2)||(node1&&!node2))return false;

        if(node1&&node2){
            if(node1.val!==node2.val)return false;

            stack1.push(node1.left);
            stack1.push(node1.right);
            stack2.push(node2.right);
            stack2.push(node2.left);
        }
    }
    return true;

};